﻿#include "widget.h"
#include "ui_widget.h"
#include <QQuickWidget>
#include <QPixmap>
#include <QBitmap>
#include <QPalette>
#include <QColor>
#include <QQmlEngine>
#include <QQmlContext>
#include <QDate>
#include <QSettings>
#include <QDir>
#include <QTextStream>
#include <QDesktopWidget>
#include <QApplication>
#include <QDebug>

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);

    //==================================================================================

//    this->setWindowFlags(this->windowFlags() | Qt::FramelessWindowHint | Qt::SubWindow);//窗口置顶，无边框

    this->setAttribute(Qt::WA_TranslucentBackground);

    this->setWindowTitle("愤怒的小鸟");

    this->setAutoFillBackground(true);//填充背景

    //================================================================================
//    QPalette palette;

//    QPixmap pixmap(":/image/toumingBg.png");

//    palette.setBrush(QPalette::Window,QBrush(pixmap));//设置笔刷为背景图片

//    this->setPalette(palette);

//    this->setFixedSize(getDesktopWidth(),getDesktopHeight());

    //================================================================================

    //不规则窗口
//    QPixmap pix;
//    pix.load(":/imamges/image/bg.png");

//    resize(pix.size());

//    setMask(pix.mask());
    //================================================================================

//    ui->quickWidget->setClearColor(QColor(Qt::transparent));    //qml 透明

    ui->quickWidget->engine()->rootContext()->setContextProperty("widget",this);

    ui->quickWidget->setSource(QUrl("qrc:/qml/main.qml"));

    this->setWindowState(Qt::WindowMaximized);

//    this->setFixedSize(410,440);

    setSystemMenu();

    Box2DPlugin plugin;
    plugin.registerTypes("Box2D");

//    move(getDesktopWidth()-410,getDesktopHeight()-40-420);
}

Widget::~Widget()
{
    delete trayIcon;
    delete ui->quickWidget;
    delete ui;
}

int Widget::getDesktopWidth()
{
    return QApplication::desktop()->width();
}

int Widget::getDesktopHeight()
{
    return QApplication::desktop()->height();
}
void Widget::mousePressEvent(QMouseEvent *event)
{
//    if(event->button()==Qt::LeftButton){
//        offset = event->globalPos() - pos();
//    }
}
void Widget::mouseMoveEvent(QMouseEvent *event)
{
//    if(event->buttons()&Qt::LeftButton){
//        QPoint temp;
//        temp = event->globalPos() - offset;
//        move(temp);
//    }
}

void Widget::mouseReleaseEvent(QMouseEvent *event)
{

}
//添加系统托盘
void Widget::setSystemMenu()
{
    trayIcon = new QSystemTrayIcon();

    trayIcon->setIcon(QIcon(":/imamges/image/logo.ico"));

    QMenu *menu = new QMenu();

    QAction *action_exit = menu->addAction("退出");

    connect(action_exit,SIGNAL(triggered(bool)),this,SLOT(systemExit(bool)));

    trayIcon->setContextMenu(menu);

    trayIcon->show();
}
void Widget::systemExit(bool b)
{
    qApp->quit();
}

QString Widget::getCurrentPath()
{
    return QDir::currentPath();
}
